package cn.wolfcode.wolf2w.business.controller.admin;

import cn.wolfcode.wolf2w.api.domain.Question;
import cn.wolfcode.wolf2w.business.query.QuestionQuery;
import cn.wolfcode.wolf2w.business.service.IQuestionService;
import cn.wolfcode.wolf2w.business.vo.QuestionCreateVO;
import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.common.core.utils.poi.ExcelUtil;
import cn.wolfcode.wolf2w.common.core.web.controller.BaseController;
import cn.wolfcode.wolf2w.common.log.annotation.Log;
import cn.wolfcode.wolf2w.common.log.enums.BusinessType;
import cn.wolfcode.wolf2w.common.security.annotation.RequiresPermissions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 问答Controller
 * 
 * @author 浪兎兎
 * @date 2025-06-07
 */
@RestController
@RequestMapping("/admin/questions")
public class QuestionAdminController extends BaseController
{
    @Autowired
    private IQuestionService questionService;

    /**
     * 查询问答列表
     */
    @RequiresPermissions("business:question:list")
    @GetMapping("/list")
    public R<IPage<Question>> list(QuestionQuery qo) {
        return R.ok(questionService.queryPage(qo));
    }


    /**
     * 导出问答列表
     */
    @RequiresPermissions("business:question:export")
    @Log(title = "问答", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Question question)
    {
        List<Question> list = questionService.list();
        ExcelUtil<Question> util = new ExcelUtil<Question>(Question.class);
        util.exportExcel(response, list, "问答数据");
    }

    /**
     * 获取问答详细信息
     */
    @RequiresPermissions("business:question:query")
    @GetMapping(value = "/{id}")
    public R<Question> getInfo(@PathVariable("id") Long id)
    {
        return R.ok(questionService.getById(id));
    }

    /**
     * 新增问答
     */
    @RequiresPermissions("business:question:add")
    @Log(title = "问答", businessType = BusinessType.INSERT)
    @PostMapping
    public R<?> add(@RequestBody Question question)
    {
        QuestionCreateVO vo = new QuestionCreateVO();
        vo.setTitle(question.getTitle());
        vo.setContents(question.getDetailDoubt());
        vo.setDestId(question.getDestinationId());
        vo.setIsDraft(question.getIsDraft());
        questionService.create(vo);
        return R.ok();
    }

    /**
     * 修改问答
     */
    @RequiresPermissions("business:question:edit")
    @Log(title = "问答", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<?> edit(@RequestBody Question question)
    {
        return toAjax(questionService.updateById(question));
    }

    /**
     * 删除问答
     */
    @RequiresPermissions("business:question:remove")
    @Log(title = "问答", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public R<?> remove(@PathVariable Long[] ids)
    {
        return toAjax(questionService.removeByIds(Arrays.stream(ids).collect(Collectors.toList())));
    }
}
